# -*- coding: utf-8 -*-
"""
@author: Tim Lux
@license: AGPL
"""

import requests
import json
from . import finn_token as finn_token
#imports for using without django
# import finn_token as finn_token

# put your token into finn_token.py like:
# token = "mytoken"
token = finn_token.token

def getQuote(symbol):
    """Returns current stock price in USD, called by stock-symbol"""
    r = requests.get('https://finnhub.io/api/v1/quote?symbol='+symbol+'&token='+token)
    tempDic = json.loads(r.text)
    #o-open h-high l-low c-current pc-previeous close |price
    return round(tempDic["c"],2)

def getAll():
    """get dictionary of all US stocks by finnhub"""
    r = requests.get('https://finnhub.io/api/v1/stock/symbol?exchange=US&token='+token)
    stocks = r.json()
    return stocks


#Unused Functions, probably later implemented
#--> goes boyond scope to this point of time
def getRecommondation(symbol):
    """Returns monthly recommendation to sell, buy or hold stock"""
    r = requests.get('https://finnhub.io/api/v1/stock/recommendation?symbol='+symbol+'&token='+token)
    tempDic = json.loads(r.text)    
    return tempDic

def getSymbol(isin):
    """Returns stock-symbol of stock-isin"""
    r = requests.get('https://finnhub.io/api/v1/stock/profile2?isin='+ str(isin))
    tempDic = json.loads(r.text)
    return tempDic["ticker"]
    
def getName(isin, symbol):
    """Returns name of company by stock- isin/symbol"""
    r = requests.get('https://finnhub.io/api/v1/stock/profile2?isin='+ str(isin))
    tempDic = json.loads(r.text)
    return tempDic["name"]


